import {
  SET_PROD_INBOUND_ORDER_LIST,
  SET_PROD_INBOUND_ORDER_LINES,
  SET_PROD_INBOUND_CURRENT_ORDER,
  SET_PROD_INBOUND_OPS_FORM_DATA,
  SET_PROD_INBOUND_ITEM_DETAILS,
  SET_PROD_INBOUND_BARCODE_DETAILS,
  DEL_PROD_INBOUND_BARCODE,
  ADD_PROD_INBOUND_ACC_QTY,
  CLEAR_PROD_INBOUND_OPS_FORM_DATA,
  SET_PROD_INBOUND_REC_LOC,
  SET_PROD_INBOUND_MIX_DATA_LIST,
} from "./action-types";

const initialState = {
  currentOrder: null,

  orderList: [],
  orderListOffset: 0,
  orderLines: [],
  orderLineOffset: 0,

  mixDataList: [],
  mixDataListOffset: 0,

  opsFormData: null,
  itemDetails: [],
  barcodeDetails: [],
};

const handleSetOrderList = (state, payload) => {
  let { items, offset } = payload;
  if (items.length === 0 && offset === 0) {
    return { ...state, orderList: [], orderListOffset: 0 };
  } else {
    return {
      ...state,
      orderList: state.orderList.concat(items),
      orderListOffset: offset
    };
  }
};

const handleSetOrderLines = (state, payload) => {
  let { items, offset } = payload;
  if (items.length === 0 && offset === 0) {
    return { ...state, orderLines: [], orderLineOffset: 0 };
  } else {
    return {
      ...state,
      orderLines: state.orderLines.concat(items),
      orderLineOffset: offset
    };
  }
};

const handleSetMixDataList = (state, payload) => {
  let { items, offset } = payload;
  if (items.length === 0 && offset === 0) {
    return { ...state, mixDataList: [], mixDataListOffset: 0 };
  } else {
    return {
      ...state,
      mixDataList: state.mixDataList.concat(items),
      mixDataListOffset: offset
    };
  }
};

const handleDelBarcode = (state, payload) => {
  return {
    ...state,
    barcodeDetails: state.barcodeDetails.filter((i) => i.id !== payload.id),
  };
};

const data = (state = initialState, action) => {
  switch (action.type) {
    case SET_PROD_INBOUND_MIX_DATA_LIST:
      return handleSetMixDataList(state, action.payload);
    case SET_PROD_INBOUND_REC_LOC:
      return {
        ...state,
        opsFormData: { ...state.opsFormData, recLoc: action.payload.items },
      };
    case CLEAR_PROD_INBOUND_OPS_FORM_DATA:
      return { ...state, opsFormData: null };
    case SET_PROD_INBOUND_ORDER_LIST:
      return handleSetOrderList(state, action.payload);
    case SET_PROD_INBOUND_ORDER_LINES:
      return handleSetOrderLines(state, action.payload);
    case SET_PROD_INBOUND_CURRENT_ORDER:
      return { ...state, currentOrder: action.payload };
    case SET_PROD_INBOUND_OPS_FORM_DATA:
      return { ...state, opsFormData: action.payload };
    case SET_PROD_INBOUND_ITEM_DETAILS:
      return { ...state, itemDetails: action.payload.items };
    case SET_PROD_INBOUND_BARCODE_DETAILS:
      return { ...state, barcodeDetails: action.payload.items };
    case DEL_PROD_INBOUND_BARCODE:
      return handleDelBarcode(state, action.payload);
    case ADD_PROD_INBOUND_ACC_QTY:
      return {
        ...state,
        opsFormData: {
          ...state.opsFormData,
          recordNum: state.opsFormData.recordNum + 1,
          lineInfo: {
            ...state.opsFormData.lineInfo,
            receivedQty: action.payload.receivedQty,
          },
        },
      };
    default:
      return state;
  }
};

export default data;
